//
//  ALPS_RecommendViewController.m
//  Alps
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ALPS_RecommendViewController.h"
#import "ALPS_RecommendTableViewCell.h"
#import "WSSNetTool.h"
#import "ALPS_MainModel.h"
#import "ALPS_ClassifyClickClickAlbumController.h"

@interface ALPS_RecommendViewController ()<UITableViewDelegate, UITableViewDataSource>
/** 主界面 */
@property (nonatomic, strong) UITableView *tableView;
/** 数据源数组 */
@property (nonatomic, strong) NSMutableArray *dataSource;
/** 网络请求页码 */
@property (nonatomic, assign) NSInteger pageNumber;
@end

@implementation ALPS_RecommendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setLeftBackButton];
    [self setMainTableView];
    [self getDataByURL:1];
}

- (void)setMainTableView
{
    self.dataSource = [NSMutableArray array];
    self.pageNumber = 1;
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"小编推荐";
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreeWidth, ScreeHeight - 64) style:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerClass:[ALPS_RecommendTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    //下拉刷新:
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self reloadData];
    }];
    
    [header setTitle:@"下拉可以刷新..." forState:MJRefreshStateIdle];
    [header setTitle:@"松开即可刷新" forState:MJRefreshStatePulling];
    [header setTitle:@"玩命加载中..." forState:MJRefreshStateRefreshing];
    
    self.tableView.mj_header = header;
    //上拉加载:
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self getMoreData];
    }];
}

- (void)getDataByURL:(NSInteger)number
{
    [WSSNetTool GetNetByURL:[NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/recommend/editor?device=iPhone&pageId=%ld&pageSize=20&statEvent=pageview%%2Falbumlist%%40小编推荐&statModule=小编推荐_更多&statPage=tab%%40发现_推荐", number] body:nil headers:nil response:WSSJSON success:^(id result) {
        NSArray *arr = [result objectForKey:@"list"];
        for (NSDictionary *dic in arr) {
            ALPS_MainModel *model = [[ALPS_MainModel alloc] initWithDic:dic];
            [self.dataSource addObject:model];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.pageNumber == [[result objectForKey:@"maxPageId"] integerValue]) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreeHeight / 6.67;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_RecommendTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = 0;
    ALPS_MainModel *model = [self.dataSource objectAtIndex:indexPath.row];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ALPS_MainModel *model = [self.dataSource objectAtIndex:indexPath.row];
    ALPS_ClassifyClickClickAlbumController *CCCAC = [[ALPS_ClassifyClickClickAlbumController alloc] init];
    CCCAC.albumId = model.albumId;
    [self.navigationController pushViewController:CCCAC animated:YES];
}

- (void)reloadData
{
    [self.dataSource removeAllObjects];
    [self getDataByURL:1];
    [self.tableView reloadData];
    self.pageNumber = 1;
}

- (void)getMoreData
{
    self.pageNumber++;
    [self getDataByURL:_pageNumber];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
